<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('主子表提交')" />
	<th:block th:include="include :: datetimepicker-css" />
	<style>
		.table_input_s{
			width: 60px;
		}
		.table_input_s1{
			width: 85px;
		}
		.table_input_s2{
			width: 70px;
		}
	</style>
</head>
<body class="gray-bg">
<div class="main-content">
	<form id="form-add" name="myForm" class="form-horizontal">
		<input name="deptId" type="hidden" id="treeId"/>
		<h4 class="form-header h4">发货单</h4>
		<div class="row">
			<div class="col-md-6">
				<div class="form-group">
					<label class="col-sm-3 control-label">发货日期：</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" name="billDate" id="billDate" placeholder="yyyy-MM-dd" lay-key="1">
					</div>
				</div>
			</div>
			<div class="col-md-6" id="divCreateBtn">
				<div class="form-group">
					<div class="col-sm-9">
						<a class="btn btn-success" id="btnCreateBillNo">生成发货单号 </a>
					</div>
				</div>
			</div>
			<div class="col-md-6" id="divBillNo" style="display: none">
				<div class="form-group">
					<label class="col-sm-3 control-label">发货单号：</label>
					<div class="col-sm-9">
						<input type="text" name="billNo" readonly class="form-control" id="billNo" placeholder="请输入文本">
					</div>
				</div>
			</div>
		</div>
		<h4 class="form-header h4"></h4>
		<div class="row">
			<div class="col-sm-12">
				<button type="button" class="btn btn-white btn-sm" onclick="chooseGoods()"><i class="fa fa-plus"> 增加</i></button>
				<button type="button" class="btn btn-white btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
				<div class="col-sm-12 select-table table-striped">
					<table id="bootstrap-table"></table>
				</div>
			</div>
		</div>
	</form>
</div>
<div class="row">
	<div class="col-sm-12">
		<ul class="sortable-list connectList agile-list">
			<li class="warning-element">
				总金额：￥<span id="totalMoney" style="color: #FF6600">0</span>
			</li>
		</ul>
	</div>
</div>
<div class="row">
	<div class="col-sm-offset-5 col-sm-10">
		<button type="button" class="btn btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i> 生成发货单</button>&nbsp;
		<!--<button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>-->
	</div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<!--<script th:src="@{/static/js/jquery.tmpl.js}"></script>-->
<script th:src="@{/js/jquery.tmpl.js}"></script>
<script th:inline="javascript">
	layui.use('laydate', function(){
		var laydate = layui.laydate;

		laydate.render({
			elem: '#billDate',
			value:new Date()
		});

	});

	$(function() {
		var datas = [[${@dict.getType('goods_colors')}]];
		var options = {
			pagination: false,
			showSearch: false,
			showRefresh: false,
			showToggle: false,
			showColumns: false,
			sidePagination: "client",
			columns: [{
				checkbox: true
			},
				{
					field: 'index',
					align: 'center',
					title: "序号",
					formatter: function (value, row, index) {
						var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
						return columnIndex + $.table.serialNumber(index);
					}
				},
				{
					field: 'name',
					align: 'center',
					title: '商品名称',
					formatter: function(value, row, index) {
						var html = $.common.sprintf("<input readonly class='form-control' type='text' name='goods[%s].name' value='%s' required:true>", index, value);
						return html;
					}
				},
				{
					field: 'barCode',
					align: 'center',
					title: '条码',
					formatter: function(value, row, index) {
						var html = $.common.sprintf("<input readonly class='form-control table_input_s1' type='text' name='goods[%s].barCode' value='%s' required:true>", index, value);
						return html;
					}
				},
				{
					field: 'tagPrice',
					align: 'center',
					title: '吊牌价',
					formatter: function(value, row, index) {
						var html = $.common.sprintf("<input id='tagPrice_"+index+"' readonly class='form-control table_input_s2' type='text' name='goods[%s].tagPrice' value='%s' placeholder='yyyy-MM-dd'>", index, value);
						return html;
					}
				},
				{
					field: 'preferentialPrice',
					align: 'center',
					title: '优惠价',
					formatter: function(value, row, index) {
						//var data = [{ index: index, type: value }];
						//return $("#goodsTypeTpl").tmpl(data).html();
						var html = $.common.sprintf("<input class='form-control table_input_s2' readonly type='text' name='goods[%s].preferentialPrice' value='%s'>", index, value);
						return html;
					}
				},
				{
					field: 'quantity',
					align: 'center',
					title: '商品数量',
					formatter: function(value, row, index) {
						var html = $.common.sprintf("<input id='quantity_"+index+"' class='form-control table_input_s' type='number' onchange='changeRows(1,this.value,"+row.tagPrice+","+row.discount+","+row.quantity+","+index+")' name='goods[%s].quantity' value='%s' digits:true>", index, value);
						return html;
					}
				},
				{
					field: 'size',
					align: 'center',
					title: '尺码',
					formatter: function(value, row, index) {
						var html = $.common.sprintf("<input id='size_"+index+"' class='form-control table_input_s2'  type='text' name='goods[%s].size' value='%s'>", index, value);
						return html;
					}
				},
				{
					field: 'money',
					align: 'center',
					title: '金额',
					formatter: function(value, row, index) {
						var html = $.common.sprintf("<input id='money_"+index+"' class='form-control table_input_s2' readonly title='金额=吊牌价*门店折扣' style='color: red' type='text' name='goods[%s].money' value='%s'>", index, value);
						return html;
					}
				},
				{
					field: 'color',
					align: 'center',
					title: '颜色',
					formatter: function(value, row, index) {
						var data = [{ index: index, type: value }];
						return $("#goodsColorTpl").tmpl(data).html();
					}
				}
			]
		};
		$.table.init(options);

		$("#barCode").focus().select();
	});



	function addColumn(name,barCode,quantity,tagPrice,discount,preferentialPrice,money,color,size) {
		var count = $("#" + table.options.id).bootstrapTable('getData').length;
		sub.editColumn();
		$("#" + table.options.id).bootstrapTable('insertRow', {
			index: count,
			row: {
				index: $.table.serialNumber(count),
				name:name,barCode:barCode,quantity:quantity,tagPrice:tagPrice,discount:discount,preferentialPrice:preferentialPrice,money:money,color:color,size:size
			}
		});
	}

	$("#btnCreateBillNo").click(function (){
		var billDate=$("#billDate").val();
		if(billDate==''){
			$.modal.msgError("发货日期不能为空");
			return;
		}

		$.ajax({
			type: "post",
			url: ctx + "goods/bill/add",
			data: {
				"billDate": billDate
			},
			success: function(data1) {
				var data=data1.billNo;
				$("#billNo").val(data);
				$("#divBillNo").show();
				$("#divCreateBtn").hide();
				//添加购物车
				/*if(data==null || data=='null'){
					$.modal.alertError('条码错误，未匹配到商品')
				}else{
					var money=data.tagPrice;
					var quantity=1;
					var discount=10;
					addColumn(data.name,data.barCode,quantity,data.tagPrice,discount,data.preferentialPrice,money,data.color);
					calcTotalMoney();
					//重新给条形码文本框焦点
					$("#barCode").focus().select();
				}*/
			}
		});


	});

	//计算折扣,改变行数据
	function changeRows(i,val,tagPrice,discount,quantity,index){
		var tp=$("#tagPrice_"+index).val();
		var d=$("#discount_"+index).val();
		var q=$("#quantity_"+index).val();
		var m=eval(tp*eval(d/10)*q);
		$("#money_"+index).val(m);
		calcTotalMoney();
	}

	//计算数量,改变行数据
	function changeRowsZk(val,tagPrice,quantity,index){
		$("input[name='goods["+index+"].money']").val(eval(tagPrice*val/10*quantity));
		calcTotalMoney();
	}
	//计算折扣,改变行数据
	function changeRowsZk(val,tagPrice,quantity,index){
		$("input[name='goods["+index+"].money']").val(eval(tagPrice*val/10*quantity));
		calcTotalMoney();
	}
	//重新计算总金额
	function calcTotalMoney(){
		var data = $("#form-add").serializeArray();
		var totalMoney=0;
		$.each(data, function(i, field){
			//$("#results").append(field.name + ":" + field.value + " ");
			if(field.name.indexOf("money")>-1){
				totalMoney=eval(totalMoney+eval(field.value));
			}
		});
		$("#totalMoney").text(totalMoney);
	}

	/* 提交表单，下单 */
	function submitHandler(index, layero){
		var prefix2 = ctx + "goods/order"
		if ($.validate.form()) {
			$.modal.confirm("确认要下单吗？",function (){
				$.operate.save(prefix2 + "/add", $("#form-add").serializeArray(),function (d){
					var orderNo=d.orderNo;
					$.modal.openTab('收银台'+orderNo, prefix2 + "/orderDetail/"+d.orderNo);
					location.reload();
				});
			});
		}
	}

	function successTabCallback(){
		console.log("毁掉函数")
	}
	var layIndex;
	function chooseGoods(){
		$.modal.open("选择服装",  "../goods/check_goods");
	}
	//子窗口回调函数
	function checkTarget(barCode){
		console.log("dddddddddddd"+barCode)
		$.modal.closeAll()

		$.ajax({
			type: "post",
			url: ctx + "goods/goods/selectGoodsBybarCode/"+barCode,
			data: {
				"barCode": barCode,
				"fragment":'fragment-tasklist'
			},
			success: function(data1) {
				var data=data1.value;
				//添加购物车
				if(data==null || data=='null'){
					$.modal.alertError('条码错误，未匹配到商品')
				}else{
					var money=data.tagPrice;
					var quantity=1;
					var discount=10;
					var size="";
					addColumn(data.name,data.barCode,quantity,data.tagPrice,discount,data.preferentialPrice,money,data.color,size);
				}
			}
		});


	}





</script>
</body>
</html>

<!-- 商品类型 -->
<script id="goodsColorTpl" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name="goods[${index}].color" th:with="type=${@dict.getType('goods_colors')}" style='width:70px'>
	<option value="">所有</option>
	<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
</select>
</div>
</script>